Okay, so in today's lesson we are going to learn how to add objects using scripts.
Okay.
So usually how we start these lessons off is if we add we add a script into the game.
And we also add a part into the game.
But sometimes the object you're wanting to use in the script might not already be there.
Um, for example, the explosion effect we learned a while ago, the explosion effect works as soon
as you join the game.
Um, which is basically because as soon as the explosion is added, it, it plays the explosion.
Okay.
So, um, a really useful way to, um, to explain adding objects is the explosion wouldn't really work
properly unless you added it in at a specific time, which you can do very easily using a script.
And we'll learn about that today.
So add a script into your game.
Okay.
And this time we don't have anything to reference because we're going to be creating a part.
Okay.
So so to create any kind of object you use a function called instance dot new okay.
So let's write that out.
It's called instance dot new like that there.
Okay.
So function.
So we're going to have a pair of brackets after it okay.
Um and this allows you to add any object into your game okay.
And there's even some objects that you can only add using this method.
So it's a very handy function to know.
Um, so the first thing you put inside these pair of brackets is the two things you could add in.
The first thing is the class name as a string.
It tells you that class name colon string.
Now class name.
I think we did discuss it very briefly at the previous section of the course, but the class name is
actually a grayed out property.
You can see just here, um, the class name describes what the object is.
So the base plate is a part for example.
So the class name is part.
So think of the class name as describing what the object is, whereas the name describes what the object
represents.
Okay.
If you have esber's base plate for example this part represents the base plate.
So its name is part.
So its name is base plate and its class name is part.
Okay.
So in speech marks because it is a string value we write in here the name, the class name of the object
you want to add.
So let's do this.
Do it simply to start off with and let's add a part into our game.
So let's type in part just like that.
There okay.
So that is the simple script on how to add a part into your game.
So let's run the game and see what happens.
And we can see that there is no part that has been added in.
You can't see a part anywhere.
And this is because of one key reason we have not given this object a parent.
Now, remember how I said ages ago that the script will never assume anything?
Well, this is one of the cases.
So we've kind of assumed that the part is going to be added into the workspace, but the script is not
going to assume that.
So you need to specifically tell it where the part should be added in in order for you to visually see
it.
So we need to change the parent of this part to the workspace.
And to do that, you can actually write it inside of its instance dot new function.
So after you have written the class name, write a comma and a space.
Make sure this comma is after the speech mark sign, because otherwise you're continuing to write the
name.
A lot of people make that mistake, so make sure it's after the speech mark ends.
Okay.
Um, and here all you need to do is reference the parent.
So we want to add it into the workspace.
So we can just reference game dot workspace like that there okay.
And this will then add a part into our game.
Now this works with most objects.
For example if you wanted to add in a folder we could do it like this.
And if you go into the workspace we can see a folder has been added there.
However, it doesn't quite work with parts just yet.
Now technically the part will be added in, but it will be deleted within a few seconds.
The reason is because it will take the default position value of 000, so it will spawn right at the
center, and the default value of can collide will be.
The default value of anchored will be false.
So it basically fall down through the map and then get killed when it gets reaches to a certain height.
Um, so we need to change some properties in order to actually visually see this part.
So to do that, we need to somehow reference this new part.
Okay.
Now we can't say game dot workspace dot part.
Okay.
The reason why we can't say that is because it doesn't exist yet.
The script won't actually won't actually let you write this until this part exists.
So it makes it quite difficult to actually reference this part.
However, the method to do it is simply by just making this bit of code here a variable.
Okay, so saying local let's call this new part is equal to instance dot new part.
Okay.
And this will when you when you when you use new part it will mean the part that you've added in.
Okay.
So some properties you want to change of the parts in order to make this a visual visual to actually
visually see it, you'd want to change anchored to be true.
Most likely you'd want to change the position.
Let's make that equal to Vector3 dot new.
Let's go for 1020.
So zero 20 zero.
So it's just above the spawn location.
Let's also change some its properties.
Let's change material dot material.
Let's change it to neon.
And let's make it a bright glowing part.
Um dot bright color is equal to bright color.
Dot new.
What brick color could we pick from?
Let's go for gold.
Okay, so it's the exact same method to change its properties.
You just start off with the new part variable, um, which is stored just here.
Okay.
And now if we run the game.
We can see that our part has been added in just there.
Okay, so when you are using this function, remember that some objects will need to have properties
changed before you can actually visually see them.
The folder it was only to parent it, but for the part you need anchor it and give it a position so
you can visually see it.
You can anchor it, which is fine, but just make sure that the position you've given it is above the
base plate, not in it or below it, because otherwise it will just fall down.
Okay.
Um, let's have a look then at the explosion example.
Okay.
So let's add in an explosion effect.
Local explosion is equal to instance dot new explosion.
Okay.
And let's parent this to the new part.
So again we don't say game dot workspace dot parts because we can't do that.
It doesn't exist yet.
So we have to instead use the variable for it.
Okay.
Um now we already know that explosions work immediately when you add them in.
So let's wait five seconds using a wait function, which will mean that that part adds in, and a couple
seconds later we can actually visually see the explosion happening.
So let's go back over here a bit.
Let's run the game and see what happens.
So the part immediately gets added in and a couple seconds time.
There we go.
An explosion occurs as well.
The explosion didn't happen at the park, though.
It happened down here in the spawn location.
So we can always go over, um, we can always add an explosion and we can see over time the position
property.
We probably need to change as well.
So let's just change that property.
Let's say explosion dot position okay.
And we could say give it the same position as we gave the new part which is 20 zero 20.
Or we could just say for explosions position is equal to the new parts position okay.
Which basically means if you change if you decide to change the position up here, it will also change
the position of the explosion.
Okay.
So it's a bit of a easier way to code it.
Pretty much.
Okay.
Or it leaves less room for errors.
Pretty much.
So.
So let's try this again.
And hopefully this time.
Oh, it added it in straight away.
Uh oh.
Because we have this in, we need to delete that.
That was just so we could see the properties.
There we go.
Perfect.
So, fascists.
An example on how insincere you can be so useful for your games.
Okay.
Um, and that's pretty much it.
It's quite simple.
It's just that extra line and then how you apply it to changing, changing properties and all that kind
of stuff.
Okay.
So have a try out with that and join me in my next lesson.
We're actually going to start off looking at local scripts.
So have a try with that and join me in the next lesson.